import router from '@/router'
import nprogress from 'nprogress'

//引入进度条样式
import 'nprogress/nprogress.css'

import useUserStore from './stores/modules/user'
import pinia from './stores'
const userStore = useUserStore(pinia)

nprogress.configure({ showSpinner: false })
//全局守卫:项目当中任意路由切换都会触发的钩子
router.beforeEach(async (to: any, from: any, next: any) => {
  //to:你将要访问那个路由 from:你从来个路由而来 next:路由的放行函数
  nprogress.start()
  const token = userStore.token
  if (token) {
    //登录成功,访问login,不能访问,指向首页
    if (to.path == '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    if (to.path == '/login') {
      next()
    } else {
      next({ path: '/login', query: { redirect: to.path } })
    }
  }
})
//全局后置守卫
router.afterEach((to: any, from: any) => {
  nprogress.done()
})
